package com.example.astronautcontact2206044090cxt1104;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class ContactSQLiteOpenHelper extends SQLiteOpenHelper {
    public ContactSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableSQL="create table if not exists contacts " +
                 "(id integer primary key autoincrement,name text not null," +
         "phone text not null,email text,organization text,notes text)";
         db.execSQL(createTableSQL);
         insertInitialData(db);

    }

    private void insertInitialData(SQLiteDatabase db) {
        AstronautContact contact = new AstronautContact("张三", "123456789", "zhangsan@gmail.com", "gdpt", "gdpt");
         ContentValues values = new ContentValues();
         values.put("name", contact.getName());
         values.put("phone", contact.getPhone());
         values.put("email", contact.getEmail());
         values.put("organization", contact.getOrganization());
         values.put("notes", contact.getNotes());
         db.insert("contacts", null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public ContactSQLiteOpenHelper(Context context){
         super(context,"astronautContact.db",null,1);
         }
}
